<?php
class ChannelModel extends CommonModel{
    protected $_validate=array(
        
    );
    
    
    //添加模型
    function addchannel(){
        $channeltype=I('post.channeltype');
        $name=I('post.name');
        $addtable=I('post.addtable');
        if($channeltype<2 || $channeltype >3){
            $this->error='模型类型不存在';
            return false;
        }
        if(empty($name)){
            $this->error='模型名不能为空';
            return false;
        }
        $data=array(
            'name'=>$name,
            'channeltype'=>$channeltype,
            'maintable'=>C('DB_PREFIX').'arctiny'
        );
        if($this->create($data)){
            if(!$channelid=$this->add()){
                return false;
            }else{
                return $channelid;
            }
        }else{
            return false;
        }
        return false;
    }
    
    //获取指定的可用模型信息
    function getUsable($cid){
        if(!$cid || !is_numeric($cid)){
            return false;
        }
        return $this->field(TRUE)->where(array('usable'=>1))->find($cid);
    }
    //生成发布页扩展表单
    function getAddonForm($fieldset,$data=array()){
        $formstr="";//表单字符串
        $start="<fieldset class='col9 f_l'>";
        $end="</fieldset>";
        $fieldset=unserialize($fieldset);
        if(!$fieldset){
            return false;
        }
        foreach($fieldset as $k=>$v){
            //单行文本
            if($v['itemtype']=='varchar' || $v['itemtype']=='int' || $v['itemtype']=='float'){
                $dvalue=isset($data[$v['fieldname']])?(string)$data[$v['fieldname']]:$v['defaultvalue'];
                $formstr.=$start."\r\n<label>{$v['itemname']}</label>\r\n<input type='text' name='addon[{$v['fieldname']}]' value='{$dvalue}' />\r\n".$end."\r\n";
                unset($dvalue);
            }
            //多行文本
            elseif($v['itemtype']=='textarea'){
                $dvalue=isset($data[$v['fieldname']])?(string)$data[$v['fieldname']]:$v['defaultvalue'];
                $formstr.=$start."\r\n<label>{$v['itemname']}</label>\r\n<textarea name='addon[{$v['fieldname']}]'>{$dvalue}</textarea>\r\n".$end."\r\n";
                unset($dvalue);
            }
            //单选
            elseif($v['itemtype']=='select'){
                $options=explode(",",$v['defaultvalue']);
                $optionstr="";
                foreach($options as $option){
                    $selected="";
                    if($data[$v['fieldname']]==$option){
                        $selected="selected";
                    }
                    $optionstr.="<option value=\"{$option}\" {$selected}>{$option}</option>\r\n";
                }
                $formstr.=$start."\r\n<label>{$v['itemname']}</label>\r\n<select name='addon[{$v['fieldname']}]'>\r\n{$optionstr}</select>\r\n".$end."\r\n";
                unset($options,$optionstr,$option,$selected);
            }
            //多选
            elseif($v['itemtype']=='checkbox'){
                 $options=explode(",",$v['defaultvalue']);
                 $dvalues=isset($data[$v['fieldname']])?explode(",",$data[$v['fieldname']]):array();
                 $optionstr="";
                 foreach($options as $option){
                     $checked="";
                     if(in_array($option,$dvalues)){
                         $checked="checked";
                     }
                     $optionstr.="<input type='checkbox' name='addon[{$v['fieldname']}][]' value=\"{$option}\" {$checked}>{$option} &nbsp;";
                 }
                 $formstr.=$start."\r\n<label>{$v['itemname']}</label>\r\n<div class='minibox'>{$optionstr}</div>\r\n".$end."\r\n";
                 unset($options,$dvalues,$optionstr,$option,$checked);
            }
            //富文本
            elseif($v['itemtype']=='html'){
                $dvalue=isset($data[$v['fieldname']])?(string)$data[$v['fieldname']]:$v['defaultvalue'];
                $formstr.=$start."\r\n<label>{$v['itemname']}</label>\r\n<div class='minibox'><textarea name=\"addon[{$v['fieldname']}]\" id=\"{$v['fieldname']}\" style=\"height:300px;width:95%;\">{$dvalue}</textarea></div>\r\n".$end."\r\n";
                $formstr.="<script>
                    KindEditor.ready(function(K) {
                        var boptions=kindeditoroptions;
                        boptions.cssData=\"pre{background-color:#ccc}\";
                        window.editor_{$v['fieldname']} = K.create('#{$v['fieldname']}',boptions);
                            
                    });
                </script>\r\n";
            }else{
                continue;
            }
        }
        return $formstr;
    }
}